package com.zat.mes.andrmes.dofun.system.adapter

import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.ClockBean
import com.zat.mes.andrmes.utils.DateUtil

/**
 * @author zy
 * @date :2020-02-16 22:04
 * @description:签到审核
 */
class ClockReadAdapter(layoutResId: Int, call: SelectFullCallBack) :
    BaseQuickAdapter<ClockBean, BaseViewHolder>(layoutResId) {

    var fullBack = call
    /**
     * 选中的数据
     */
    val selectList: MutableList<ClockBean> = ArrayList()

    override fun convert(helper: BaseViewHolder, item: ClockBean) {
        helper.setText(R.id.tvNameClockReadItem, item?.empName)
            .setText(
                R.id.tvTimeClockReadItem,
                DateUtil.formatDate(DateUtil.strToDateLong(item?.loginTime), "HH:mm")
            )
        if ("1".equals(item?.type)) {
            //上班
            helper.setText(R.id.tvTypeClockReadItem, context.getString(R.string.text_up))
            helper.getView<TextView>(R.id.tvTypeClockReadItem)
                .setBackgroundColor(ContextCompat.getColor(context, R.color.color_theme))
        } else {
            //下班
            helper.setText(R.id.tvTypeClockReadItem, context.getString(R.string.text_down))
            helper.getView<TextView>(R.id.tvTypeClockReadItem)
                .setBackgroundColor(ContextCompat.getColor(context, R.color.color_yellow))
        }
        val ivSelect = helper.getView<ImageView>(R.id.ivSelectClockReadItem);
        if (selectList.contains(item)) {
            ivSelect.background = context.resources.getDrawable(R.mipmap.icon_kaoqin_xuanzhong)
        } else {
            ivSelect.background = context.resources.getDrawable(R.mipmap.icon_kaoqin_weixuanzhong)
        }
        helper.itemView.setOnClickListener {
            if (selectList.contains(item)) {
                selectList.remove(item)
                fullBack.selectFull(false)
                notifyDataSetChanged()
            } else {
                selectList.add(item!!)
                if (selectList.size == data.size) {
                    setSelectList(true)
                    fullBack.selectFull(true)
                } else {
                    notifyDataSetChanged()
                }
            }


        }


    }

    /**
     * 设置选择数据
     */
    fun setSelectList(full: Boolean?) {
        full?.let {
            if (it) {
                selectList.clear()
                selectList.addAll(data)
            } else {
                selectList.clear()
            }
            notifyDataSetChanged()

        }
    }

    /**
     * 全选监听
     */
    interface SelectFullCallBack {
        fun selectFull(full: Boolean)
    }
}